import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:verse_systems/commom/models/contact_model.dart';
import 'package:verse_systems/pages/authentication/authentication_control.dart';
import 'package:verse_systems/res/colors.dart';

import 'package:verse_systems/utils/image_utils.dart';
import 'package:verse_systems/widgets/drawer_appbar.dart';
import 'package:verse_systems/widgets/sp_button.dart';

class AuthenticationInfo extends StatefulWidget {
  const AuthenticationInfo({super.key});

  @override
  State<AuthenticationInfo> createState() => _AuthenticationInfoState();
}

class _AuthenticationInfoState extends State<AuthenticationInfo> {
  final control = Get.find<AuthenticationControl>();
  final state = Get.find<AuthenticationControl>().state;

  @override
  void initState() {
    super.initState();
    control.getContactInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.system_background_color,
      appBar:  DrawerAppbar(
        title: 'essential_info'.tr,
      ),
      body: GetBuilder<AuthenticationControl>(
          init: control,
          builder: (_) {
            return GestureDetector(
                onTap: () {
                  FocusScope.of(context).unfocus();
                },
                child: CustomScrollView(
                  slivers: [
                    SliverToBoxAdapter(child: _information),
                    SliverToBoxAdapter(child: SizedBox(height: 8.w)),
                    SliverList(
                        delegate: SliverChildBuilderDelegate((context, index) {
                      ContactModel model = state.contact[index];
                      return _contactGrid(model: model, index: index);
                    }, childCount: state.contact.length)),
                    SliverToBoxAdapter(child: _next),
                  ],
                ));
          }),
    );
  }

  Widget get _next {
    return Container(
      margin: EdgeInsets.fromLTRB(25.w, 52.w, 25.w, 65.w),
      width: 310.w,
      child: SPButton(
        minHeight: 50.w,
        fontSize: 13.sp,
        text: "next".tr,
        textColor: Colours.app_main_color,
        backgroundColor: Colours.app_main_background_color,
        radius: 25.w,
        onPressed: control.uploadBasicInfo,
      ),
    );
  }

  Widget _contactGrid({required ContactModel model, required int index}) {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        width: MediaQuery.of(context).size.width,
        color:
            model.title == null ? Colors.transparent : Colours.app_main_color,
        child: InkWell(
            onTap: () {
              control.handlePersonalInfo(index: index, model: model);
            },
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      model.title == null
                          ? model.sub.toString()
                          : model.title.toString(),
                      style: TextStyle(
                        color: Colours.app_forward_color,
                        fontSize: model.title == null ? 12.sp : 16.sp,
                      ),
                    ),
                    (model.value == "详细地址街道、门牌号等")
                        ? Expanded(
                            child: TextField(
                            keyboardType: TextInputType.text,
                            controller: (model.subs == "详细地址街道1")
                                ? control.user_city_of_residence_edit
                                : control.user_city_of_work_edit,
                            textAlign: TextAlign.right, //文本对齐方式
                            style: TextStyle(
                              fontSize: 15.sp,
                              color: Colours.app_forward_color,
                            ),
                            decoration: InputDecoration(
                                contentPadding:
                                    EdgeInsets.symmetric(vertical: 5.w),
                                border: InputBorder.none,
                                hintText: model.value,
                                counterText: '',
                                hintStyle: TextStyle(
                                  fontSize: 15.sp,
                                  color: Colours.app_text_color,
                                )),
                            showCursor: true,
                            cursorColor: Colours.app_main_background_color,
                            onChanged: (v) {},
                          ))
                        : Row(
                            children: [
                              model.title == null
                                  ? const SizedBox()
                                  : SizedBox(
                                      height: 44.w,
                                      child: Row(
                                        children: [
                                          Text(
                                            model.value.toString(),
                                            style: TextStyle(
                                              color: (model.value == "请选择") ||
                                                      (model.value == "手机号")
                                                  ? Colours.app_line_color
                                                  : Colours.app_forward_color,
                                              fontSize: 15.sp,
                                            ),
                                          ),
                                          model.icon == null
                                              ? Icon(
                                                  Icons
                                                      .arrow_forward_ios_rounded,
                                                  color: Colours.app_line_color,
                                                  size: 18.w,
                                                )
                                              : Image.asset(
                                                  ImageUtils.getImgPath(
                                                      "phone_icon"),
                                                  width: 22.w,
                                                  height: 22.w,
                                                ),
                                        ],
                                      ))
                            ],
                          )
                  ],
                ),
                Container(
                  height: 1.w,
                  color: Colours.app_text_line_color,
                ),
              ],
            )));
  }

  Widget get _information {
    return Container(
        width: MediaQuery.of(context).size.width,
        color: Colours.app_main_color,
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 24.w),
            Row(
              children: [
                Image.asset(
                  ImageUtils.getImgPath("information"),
                  fit: BoxFit.fill,
                  width: 38.w,
                  height: 30.w,
                ),
                Text(
                  'essential_info'.tr,
                  style: TextStyle(
                      color: Colours.app_black_text_color,
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w600),
                ),
              ],
            ),
            Container(
                margin: EdgeInsets.fromLTRB(0, 14.w, 0, 12.w),
                child: Text(
                  state.notice_infor,
                  style: TextStyle(
                    color: Colours.app_text_color,
                    fontSize: 12.sp,
                  ),
                )),
          ],
        ));
  }
}
